import { Controller } from "../../mvc/core/Controller";
import { ISpiritEquipController } from "./interfaces/ISpiritEquipController";
import { SpiritEquipCoinReturnWindow } from "../view/spiritEquip/SpiritEquipCoinReturnWindow";
import { IView } from "../../mvc/interfaces/IView";
import { SpiritEquipModule } from "../../../modules/SpiritEquipModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SPlayerItem } from "../../../Message/Public/SPlayerItem";
import { ItemData } from "../resource/info/ItemData";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SSpiritCoinReturnInfo } from "../../../Message/Public/SSpiritCoinReturnInfo";
import { ISpiritEquipModule } from "../../../modules/interfaces/ISpiritEquipModule";
type int = number;
//class SpiritEquipController
    
    export  class SpiritEquipController extends Controller implements ISpiritEquipController
    {
       

        public get returnWindow(): SpiritEquipCoinReturnWindow
        {
            if (!this._returnWindow) 
            {
                this._returnWindow = new SpiritEquipCoinReturnWindow();
            }
            return this._returnWindow;
        }

        protected /* override */ initView(): IView
        {
            if (this._spiritEquipWindow == null) 
            {
                this._spiritEquipWindow = new SpiritEquipModule();
            }
            return this._spiritEquipWindow;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.SpiritEquipActivate, this.onSpiritEquipActivateHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipActivateSuccess, this.onSpiritEquipActivateSuccessHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipWindowOpen, this.onSpiritEquipWindowOpenHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipStrength, this.onSpiritEquipStrengthHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipStrengthSuccess, this.onSpiritEquipStrengthSuccessHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipEat, this.onSpiritEquipEatHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipEatSuccess, this.onSpiritEquipEatSuccessHandler);
            Dispatcher.addEventListener(EventName.SpiritEquipShowReturnWindow, this.onSpiritEquipShowReturnWindowHandler);
            NetDispatcher.addCmdListener(ServerCommand.SpiritEquipInfoUpdate, this.onSpiritEquipInfoUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.RolepackDataChange, this.onRolePackItemsChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.RolePackItemsChange, this.onRolePackItemsChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSpiritCoinReturn, this.onSpiritCoinReturnHandler);
            return;
        }

        /* internal  */onSpiritEquipWindowOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            view.show();
            this._spiritEquipWindow.showToTab(loc1);
            this._spiritEquipWindow.updateMoney();
            this._spiritEquipWindow.updateEquipList();
            return;
        }

        /* internal  */onSpiritEquipActivateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.equipmentProxy.activateSpiritEquip(loc1);
            return;
        }

        /* internal  */onSpiritEquipActivateSuccessHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onSpiritEquipStrengthHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["equipUid"] as string;
            var loc2=arg1.data["posType"] as int;
            GameProxy.equipmentProxy.spiritEquipStrength(loc1, loc2);
            return;
        }

        /* internal  */onSpiritEquipStrengthSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["resultPlayerItem"] as SPlayerItem;
            var loc2=arg1.data["result"] as int;
            if (this._spiritEquipWindow && !this._spiritEquipWindow.isHide && loc1) 
            {
                this._spiritEquipWindow.updateStrengthData(new ItemData(loc1));
            }
            if (loc2) 
            {
                MsgManager.showRollTipsMsg(Language.getString(36634));
            }
            return;
        }

        /* internal  */onSpiritEquipEatHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.equipmentProxy.swallowSpiritEquip(loc1);
            return;
        }

        /* internal  */onSpiritEquipEatSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(36645));
            return;
        }

        /* internal  */onSpiritEquipShowReturnWindowHandler(arg1: DataEvent): void
        {
            if (!this._isTipRerurn && cache.spiritEquip.spiritCoinReturnInfo) 
            {
                this.returnWindow.show();
                this.returnWindow.updateByCoinReturnInfo(cache.spiritEquip.spiritCoinReturnInfo);
                this._isTipRerurn = true;
            }
            return;
        }

        /* internal  */onSpiritEquipInfoUpdateHandler(arg1: Object): void
        {
            if (this._spiritEquipWindow && !this._spiritEquipWindow.isHide) 
            {
                this._spiritEquipWindow.updateEquipList();
                this._spiritEquipWindow.spiritEquipUpdate();
            }
            return;
        }

        /* internal  */onRolePackItemsChangeHandler(arg1: Object): void
        {
            if (this._spiritEquipWindow && !this._spiritEquipWindow.isHide) 
            {
                this._spiritEquipWindow.updateEquipList();
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            if (this._spiritEquipWindow && !this._spiritEquipWindow.isHide) 
            {
                this._spiritEquipWindow.updateMoney();
            }
            return;
        }

        /* internal  */onSpiritCoinReturnHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSpiritCoinReturnInfo;
            cache.spiritEquip.spiritCoinReturnInfo = loc1;
            this.onSpiritEquipShowReturnWindowHandler(null);
            return;
        }

        private /* var */_spiritEquipWindow: ISpiritEquipModule;

        private /* var */_returnWindow: SpiritEquipCoinReturnWindow;

        private /* var */_isTipRerurn: Boolean;
    }
